/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: DocumentPublisher.java
 * @Package phoenics.apeak.crawl.service
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:31
 * @version V1.0
 */
package phoenics.apeak.crawl.service;

import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.lmax.disruptor.dsl.Disruptor;

import phoenics.apeak.crawl.Document;
import phoenics.apeak.crawl.DocumentTranslator;
import phoenics.apeak.crawl.Extractor;
// TODO: Auto-generated Javadoc

/**
 * The Class DocumentPublisher.
 *
 * @Description The Class DocumentPublisher.
 */
@Service
public class DocumentPublisher implements Publisher, ApplicationContextAware {

	/** The application context. */
	private ApplicationContext applicationContext;

	/** The disruptor. */
	@Autowired
	private Disruptor<Document> disruptor;

	/**
 * Sets the disruptor.
 *
 * @param disruptor
 *            the new disruptor
 */
	public void setDisruptor(Disruptor<Document> disruptor) {
		this.disruptor = disruptor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.context.ApplicationContextAware#setApplicationContext
	 * (org.springframework.context.ApplicationContext)
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see phoenics.apeak.crawl.service.Publisher#publish(java.lang.Class,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public void publish(Class<? extends Extractor> extractorClass, String url, String params) {
		publish(extractorClass, url, params, false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see phoenics.apeak.crawl.service.Publisher#publish(java.lang.Class,
	 * java.lang.String)
	 */
	@Override
	public void publish(Class<? extends Extractor> extractorClass, String url) {
		publish(extractorClass, url, null);
	}

	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.service.Publisher#publish(java.lang.Class, java.lang.String, java.lang.String, boolean)
	 */
	@Override
	public void publish(Class<? extends Extractor> extractorClass, String url, String params, boolean repeat) {
		if (extractorClass == null) {
			return;
		}

		if (StringUtil.isBlank(url)) {
			return;
		}
		disruptor.publishEvent(new DocumentTranslator(this.applicationContext, extractorClass, url, params, repeat));

	}

	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.service.Publisher#publish(java.lang.Class, java.lang.String, boolean)
	 */
	@Override
	public void publish(Class<? extends Extractor> extractorClass, String url, boolean repeat) {
		publish(extractorClass, url, null, repeat);
	}

}
